import c_screener_mode
import pygame

options_slider_image=pygame.image.load("options_slider.bmp")
options_slider_image.set_colorkey((255,0,255))

class OptionsSlider:
	def __init__(self,gears,cur_value,min_value,max_value,step_value):
		self.gears=gears
		self.cur_value=cur_value
		self.min_value=min_value
		self.max_value=max_value
		self.step_value=step_value
	def inc(self):
		if self.cur_value+self.step_value<=self.max_value:
			self.cur_value=self.cur_value+self.step_value
			self.changeConfirmed()
			return True
		return False
	def dec(self):
		if self.cur_value-self.step_value>=self.min_value:
			self.cur_value=self.cur_value-self.step_value
			self.changeConfirmed()
			return True
		return False
class MusicVolumeSlider(OptionsSlider):
	def __init__(self,gears):
		OptionsSlider.__init__(self,gears,gears.music_volume,0.0,1.0,0.1)
	def changeConfirmed(self):
		self.gears.setNewMusicVolume(self.cur_value)
class ScreenSizeSlider(OptionsSlider):
	def __init__(self,gears):
		size_list=pygame.display.list_modes()
		OptionsSlider.__init__(self,gears,size_list.index(gears.screen_size),0,len(size_list)-1,1)
		self.size_list=size_list
	def changeConfirmed(self):
		self.gears.screen_size=self.size_list[self.cur_value]
		self.gears.makeWindow()

class cOptionsScreen(c_screener_mode.c_screener_mode):
	def __init__(self):#,gears):
		c_screener_mode.c_screener_mode.__init__(self)
		#self.gears=gears
		self.font = pygame.font.SysFont("Courier", 40)
		self.menu_title=""
		#self.options={}
		#self.options[1]=self.gears.music_volume
	def makeMenu(self):
		self.menuSurface.fill((0, 0, 0))
		for i in range(0,len(self.options)):
			w=self.window_size[0]*self.options[i].cur_value/self.options[i].max_value
			self.menuSurface.blit(options_slider_image,(w-0.5*options_slider_image.get_width(),i*options_slider_image.get_height()))
	def draw(self):
		self.gears.screen.blit(self.getBackground(),(0,0))
		self.gears.screen.blit(self.menuSurface,(0,0))
		pygame.display.flip()
	def getBackground(self):
		bg=pygame.Surface(self.menuSurface.get_size())
		bg.fill((40,0,0))
		return bg
	def locali_check(self):
		if self.locali!=self.gears.locali:
			self.locali=self.gears.locali
	def configure_window(self):
		self.window_size=self.gears.screen_size
		self.menuSurface=pygame.Surface(self.window_size)
		self.menuSurface.set_colorkey( (0, 0, 0) )
	def activate(self):
		#self.setPointer(0)
		#self.options={}
		self.pointer=0
		self.options=[]
		self.options.append(MusicVolumeSlider(self.gears))
		self.options.append(ScreenSizeSlider(self.gears))
		self.makeMenu()
	def do(self):
		g=pygame.event.get()
		remake=False
		for event in g:
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					self.switch_mode(None)
				if event.key == pygame.K_LEFT:
					remake=remake or self.options[self.pointer].dec()
				if event.key == pygame.K_RIGHT:
					remake=remake or self.options[self.pointer].inc()
				if event.key == pygame.K_UP:
					if self.pointer>0:
						self.pointer=self.pointer-1
						remake=True
				if event.key == pygame.K_DOWN:
					if self.pointer+1<len(self.options):
						self.pointer=self.pointer+1
						remake=True
				'''if event.key == pygame.K_KP_ENTER or event.key == 13:
					self.switch_mode(self.submenus_link[self.pointer])'''
		if remake:
			self.gears.writeSettings()
			self.window_check()
			self.makeMenu()
		self.draw()